#include <QCoreApplication>
#include <QtDBus>
#include <QVariantMap>

#include "bike_interface.h"
#include "dbus_signal_test.h"

#define BIKE_SERVICE "com.readygo"
#define BIKE_SERVICE_PATH "/"
#define BIKE_INTERFACE  "com.readygo.bike"


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

#if 0    /* process by qdbusxml2cpp */
    DbusSignalTest *test = new DbusSignalTest();
    com::readygo::bike *bikeIface;

    /* defined in bike_interface.h */
    bikeIface = new com::readygo::bike(BIKE_SERVICE, BIKE_SERVICE_PATH,
                                       QDBusConnection::sessionBus());

    /* test the signal */
    bikeIface->connect(bikeIface, SIGNAL(SensorDataChanged(const QVariantMap &)),
            test, SLOT(onSensorDataChanged(const QVariantMap &)));

    QDBusPendingReply<QString> mapPathReply = bikeIface->GetMapDataPath();
    mapPathReply.waitForFinished();
    if (mapPathReply.isValid()) {
        qDebug() << mapPathReply.value();
    } else {
        qDebug() << "GetMapDataPath invalid";
    }

    QDBusPendingReply<QString>encryReply = bikeIface->GetEncrypData();
    encryReply.waitForFinished();
    if (encryReply.isValid()) {
        qDebug() << encryReply.value();
    } else {
        qDebug() << "getEncrypData invalid";
    }

    QDBusPendingReply<QVariantMap> getSensorReply = bikeIface->GetSensorData();
    getSensorReply.waitForFinished();
    if (getSensorReply.isValid()) {
        QVariantMap sensorData = getSensorReply.value();
        qDebug() << sensorData;
        if (sensorData.contains("power")) {
            qDebug() << "power:" << sensorData.value("power").toUInt();
        }
        if (sensorData.contains("angle")) {
            qDebug() << "angle:" << sensorData.value("angle").toInt();
        }
        if (sensorData.contains("velocity")) {
            qDebug() << "velocity:" << sensorData.value("velocity").toUInt();
        }
    } else {
        qDebug() << "GetSensorData invalid";
    }
#else
    /* 1. connect to session dbus */
    DbusSignalTest *test = new DbusSignalTest();
    QDBusConnection session  = QDBusConnection::sessionBus();

    if (session.isConnected()) {
        /* 2. create dbus interface */
        QDBusInterface *iface = new QDBusInterface(BIKE_SERVICE, BIKE_SERVICE_PATH,
                                    BIKE_INTERFACE, QDBusConnection::sessionBus());
        if (iface->isValid()) {
            /* 3. connect the signal */
            session.connect(BIKE_SERVICE, BIKE_SERVICE_PATH, BIKE_INTERFACE, "SensorDataChanged",
                            test, SLOT(onSensorDataChanged(const QVariantMap &)));
            /* 4. call the method */
            QDBusReply<QString> getMapPathReply = iface->call("GetMapDataPath");
            if (getMapPathReply.isValid()) {
                qDebug() << getMapPathReply.value();
            } else {
                qDebug() << "get map data path invalid";
            }

            QDBusReply<QString> getEncrypReply = iface->call("GetEncrypData");
            if (getEncrypReply.isValid()) {
                qDebug() << getEncrypReply.value();
            } else {
                qDebug() << "getEncrypReply invalid";
            }

            QDBusReply<QVariantMap> getSensorDataReply = iface->call("GetSensorData");
            if (getSensorDataReply.isValid()) {
                QVariantMap sensorData = getSensorDataReply.value();
                qDebug() << sensorData;
                if (sensorData.contains("power")) {
                    qDebug() << "power:" << sensorData.value("power").toUInt();
                }
                if (sensorData.contains("angle")) {
                    qDebug() << "angle:" << sensorData.value("angle").toInt();
                }
                if (sensorData.contains("velocity")) {
                    qDebug() << "velocity:" << sensorData.value("velocity").toUInt();
                }
             } else {
                qDebug() << "get sensor data invalid";
             }
        } else {
            qDebug() << "iface invalid";
        }
    } else {
        qDebug() << "dbus session connected failed";
    }
#endif

    return a.exec();
}
